package cate.game.play.skill.passive.fairy;

import cate.common.table.d.GDSkill;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.move.MoveCtx;
import cate.game.play.proce.result.DeadResult;
import cate.game.play.skill.FighterCD;
import cate.game.play.skill.Skill;
import cate.game.play.skill.passive.PassiveHandler;

/**
 * 效果1：血量低于5%直接斩杀，斩杀伤害不超过攻击力的5倍
 * 效果2：双子死亡后以【灵魂】状态重返战场，并在双子出手回合时释放一次“倒转乾坤”，随后消散。冷却5回合
 */
public class 双子灵器PH extends PassiveHandler {

	private int 斩杀血量;

	private double 斩杀攻击系数;

	private int 灵魂buff;

	private int 技能原型;

	private int 冷却回合;

	//斩杀血量=5000&斩杀攻击系数=50000&
	//灵魂buff=？？&技能原型=？？冷却回合=5
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		斩杀血量 = args.getInt("斩杀血量", 0);
		斩杀攻击系数 = args.getWDouble("斩杀攻击系数", 0d);
		灵魂buff = args.getInt("灵魂buff", 0);
		技能原型 = args.getInt("技能原型", 0);
		冷却回合 = args.getInt("冷却回合", 0);
	}


	private Skill appendSkill;

	@Override
	public void onFirstEnter(ActionCtx action) {
		appendSkill = skill.owner.skill.getActiveSkillByProtoId(技能原型);
	}

	@Override
	public void afterOneAttack(SkillActionCtx action, long totalDamage, boolean strike, Fighter target) {
		if (斩杀血量 < target.attr.total().hpCurRate()) {
			return;
		}
		if (skill.owner.attr.attTotal() * 斩杀攻击系数 < target.attr.total().hpCur()) {
			return;
		}
		target.attr.kill(action, skill.owner);
	}

	private int lastDeadRound;

	@Override
	public void immediateRebirth(ActionCtx action) {
		if (appendSkill == null) {
			return;
		}
		if (FighterCD.inCD(lastDeadRound, 冷却回合, action.getRound().index)) {
			return;
		}
		lastDeadRound = action.getRound().index;
		skill.owner.attr.doResurrect(action, 1d);
		skill.owner.buff.tryAddByTid(action, 灵魂buff);
		appendSkill.param.setIgnCtrlMove(true);
		skill.owner.status.setTurnSkill(appendSkill);
		skill.owner.status.getGhost().addSkill(skill.tid);
	}

	@Override
	public void onOwnerMoveDone(MoveCtx move, TimeActionCtx action) {
		if (!skill.owner.status.getGhost().containSkill(skill.tid)) {
			return;
		}
		if (move.getSkill().cfg.row.protoId != 技能原型) {
			return;
		}
		skill.owner.status.getGhost().removeSkill(skill.tid);
		skill.owner.buff.removeBuffByTid(action, 灵魂buff);
		skill.owner.attr.hpCur(0d);
		skill.owner.setDead(true);
		skill.owner.status.setMarkDead(true);
		action.addResult(new DeadResult().pidSet(skill.owner.getPid()));
	}

	@Override
	public int priority() {
		return GDSkill.Priority.HIGH;
	}
}
